package chpt10;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

public class MenuMainFrame extends JFrame {
    private JTextArea textArea;

    public MenuMainFrame() {
    	try {
    	    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    	} catch (Exception e) {
    	    e.printStackTrace();
    	}
        setTitle("菜单示例");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // 创建菜单栏
        JMenuBar menuBar = new JMenuBar();

        // 创建文件菜单
        JMenu fileMenu = new JMenu("文件");
        fileMenu.setMnemonic(KeyEvent.VK_F);

        // 创建打开菜单项
        JMenuItem openItem = new JMenuItem("打开");
        openItem.setMnemonic(KeyEvent.VK_O);
        openItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                int result = fileChooser.showOpenDialog(MenuMainFrame.this);
                if (result == JFileChooser.APPROVE_OPTION) {
                    File selectedFile = fileChooser.getSelectedFile();
                    try {
                        String content = new String(Files.readAllBytes(selectedFile.toPath()));
                        textArea.setText(content);
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });

        // 创建保存菜单项
        JMenuItem saveItem = new JMenuItem("保存");
        saveItem.setMnemonic(KeyEvent.VK_S);
        saveItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                int result = fileChooser.showSaveDialog(MenuMainFrame.this);
                if (result == JFileChooser.APPROVE_OPTION) {
                    File selectedFile = fileChooser.getSelectedFile();
                    try {
                        String content = textArea.getText();
                        Files.write(selectedFile.toPath(), content.getBytes());
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });
        
        // 创建退出菜单项
        JMenuItem exitItem = new JMenuItem("退出");
        exitItem.setMnemonic(KeyEvent.VK_X);
        exitItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        // 创建编辑菜单
        JMenu editMenu = new JMenu("编辑");
        editMenu.setMnemonic(KeyEvent.VK_E);

        // 创建复制菜单项
        JMenuItem copyItem = new JMenuItem("复制");
        copyItem.setMnemonic(KeyEvent.VK_C);
        copyItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.copy();
            }
        });

        // 创建粘贴菜单项
        JMenuItem pasteItem = new JMenuItem("粘贴");
        pasteItem.setMnemonic(KeyEvent.VK_P);
        pasteItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.paste();
            }
        });

        // 创建帮助菜单
        JMenu helpMenu = new JMenu("帮助");
        helpMenu.setMnemonic(KeyEvent.VK_H);

        // 创建关于菜单项
        JMenuItem aboutItem = new JMenuItem("关于");
        aboutItem.setMnemonic(KeyEvent.VK_A);
        aboutItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(MenuMainFrame.this, "这是About对话框。", "关于", JOptionPane.INFORMATION_MESSAGE);
            }
        });

        // 将菜单项添加到对应的菜单中
        fileMenu.add(openItem);
        fileMenu.add(saveItem);
        fileMenu.addSeparator();
        fileMenu.add(exitItem);
        editMenu.add(copyItem);
        editMenu.add(pasteItem);
        helpMenu.add(aboutItem);

        // 将菜单添加到菜单栏中
        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        menuBar.add(helpMenu);

        // 创建文本区域
        textArea = new JTextArea();

        // 将菜单栏设置为顶级容器的菜单栏
        setJMenuBar(menuBar);

        // 将文本区域添加到顶级容器中
        add(new JScrollPane(textArea), BorderLayout.CENTER);

        // 设置顶级容器的大小和可见性
        setSize(500, 300);
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                MenuMainFrame frame = new MenuMainFrame();
                frame.setVisible(true);
            }
        });
    }
}